const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Resource = sequelize.define('Resource', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  type: {
    type: DataTypes.ENUM('video', 'document', 'link', 'tool', 'book', 'course'),
    allowNull: false
  },
  category: {
    type: DataTypes.STRING(50),
    allowNull: false,
    comment: 'HTML/CSS/JavaScript/C#/Vue/CEF3/其他'
  },
  url: {
    type: DataTypes.STRING(500),
    allowNull: false
  },
  thumbnail: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '缩略图'
  },
  difficulty: {
    type: DataTypes.ENUM('beginner', 'intermediate', 'advanced'),
    defaultValue: 'beginner'
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '标签数组'
  },
  is_free: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  },
  language: {
    type: DataTypes.STRING(10),
    defaultValue: 'zh',
    comment: '资源语言'
  },
  view_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  like_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  created_by: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'users',
      key: 'id'
    }
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  },
  sort_order: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '排序权重'
  }
}, {
  tableName: 'learning_resources',
  indexes: [
    {
      fields: ['category', 'type']
    },
    {
      fields: ['difficulty']
    },
    {
      fields: ['is_active', 'sort_order']
    }
  ]
});

module.exports = Resource; 